package com.tetris.project;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import engine.Score;

public class HandleScores {
	
	//ArrayList<Score> profileList;
	final String PROFILES_FILE = "/data/data/com.tetris.project/profiles";
	//int profileIndex;
	Context context;
	
	public HandleScores (Context c){
		context = c;
	}
	
	public ArrayList<Score> readProfiles(ArrayList<Score> profileList, String fileName) {
		//ArrayList<Score> profileList = new ArrayList<Score>();
		try {
			DataInputStream dis = new DataInputStream(new FileInputStream(fileName));
			ObjectInputStream ois = new ObjectInputStream(dis);
			Object o = null;
			while ( (o = ois.readObject()) != null ) {
				profileList.add((Score) o);
			}
			ois.close();
		} catch (EOFException e) {
			Log.d("Read File", "EOFExcpetion");
			Shr_Res.isScoreListEmpty = false;
			Log.d("Read File", "isScoreListEmpty = "+Shr_Res.isScoreListEmpty);
			e.printStackTrace();
		} catch (StreamCorruptedException e) {
			// TODO Auto-generated catch block
			Log.d("Read File", "StreamCorruptedException");
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			Log.d("Read File", "FileNotFoundException");
			e.printStackTrace();
			Shr_Res.isScoreListEmpty = true;
			Log.d("Read File", "isScoreListEmpty = "+Shr_Res.isScoreListEmpty);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Log.d("Read File", "IOException");
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			Log.d("Read File", "ClassNotFoundException");
			e.printStackTrace();
		}
		return profileList;
	}
	
	public void writeProfiles(ArrayList<Score> profileList, String fileName) {
		try {
		DataOutputStream dos = new DataOutputStream(new FileOutputStream(fileName));
			ObjectOutputStream oos = new ObjectOutputStream(dos);
			for ( int i = 0; i < profileList.size(); i++) {
				oos.writeObject(profileList.get(i));
			}
			oos.close();
		} catch (EOFException e) {
			Log.d("Write File", "EOFExcpetion");
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			Log.d("Write File", "FileNotFoundException");
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Log.d("Write File", "IOException");
			e.printStackTrace();
		}
	}
	
}